/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ems.lib;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author hoa
 */
public class Utility {

    public Utility() {
    }

    /**
     * Clear screen
     */
    public void clearScreen() {
        String clearScreenCommand = null;
        if (System.getProperty("os.name").startsWith("Window")) {
            clearScreenCommand = "cls";
        } else {
            clearScreenCommand = "clear";
        }

        try {
            Runtime.getRuntime().exec(clearScreenCommand);
            System.out.print("\033c");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Return the resultSet to a List
     * @param list
     * @param resultSet
     * @throws SQLException
     */
    public void resultSetToList(List list, ResultSet resultSet) throws SQLException {
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();

        //Loop through the ResultSet data rows and add each one to the List
        while (resultSet.next()) {
            list.add(getRowData(resultSet, columnCount));
        }

    }

    /**
     * Return resultSet to 2 dimensional object array
     * @param resultSet
     * @return object[][]
     * @throws SQLException
     */
    public Object[][] resultSetToObjectArray(ResultSet resultSet) throws SQLException {
        List list = new ArrayList();
        resultSetToList(list, resultSet);
        Object[][] arr = listToObjectArray(list);
        return arr;
    }

    /**
     * Get column names from ResultSet
     * @param resultSetMetaData
     * @return array of String
     * @throws SQLException
     */
    public String[] getColumnNames(ResultSetMetaData resultSetMetaData) throws SQLException {
        int columnCount = resultSetMetaData.getColumnCount();
        String columnNames[] = new String[columnCount];

        for (int colIndex = 1; colIndex
                <= columnCount; colIndex++) {
            columnNames[colIndex - 1] = resultSetMetaData.getColumnName(colIndex);
        }

        return columnNames;
    }

    /**
     * Convert a list to Object Array
     * @param list
     * @return object 2 dimensional array
     */
    public Object[][] listToObjectArray(List list) {
        if (list == null || list.isEmpty()) {
            return null;
        } else {
            Object[][] resultArr = new Object[list.size()][];
            list.toArray(resultArr);

            return resultArr;
        }
    }

    /**
     * Get row data  from Result Set
     * @param resultSet
     * @param columnCount
     * @return
     * @throws SQLException
     */
    private Object[] getRowData(ResultSet resultSet, int columnCount) throws SQLException {
        Object rowData[] = new Object[columnCount];

        for (int colIndex = 1; colIndex <= columnCount; colIndex++) {
            rowData[colIndex - 1] = resultSet.getObject(colIndex);
        }
        return rowData;
    }
}
